﻿using System.Collections.Generic;
using System.Text;
using Dapper;
using Eagles.Application.Model.Audit.Requset;
using Eagles.Base.Cache;
using Eagles.Base.DataBase;
using Eagles.DomainService.Model.Audit;
using Eagles.DomainService.Model.Score;
using Eagles.DomainService.Model.User;
using Eagles.Interface.DataAccess;

namespace Eagles.DomianService.DataAccess
{
   public  class AuditDataAccess: IAuditDataAccess
    {
        private readonly IDbManager dbManager;

        private readonly ICacheHelper cacheHelper;

        public AuditDataAccess(IDbManager dbManager,ICacheHelper cacheHelper)
        {
            this.dbManager = dbManager;
            this.cacheHelper = cacheHelper;
        }
        public int CreateAudit(TbReview mod)
        {
            return dbManager.Excuted(@"INSERT INTO `eagles`.`tb_review`
(
`OrgId`,
`BranchId`,
`NewsId`,
`NewsType`,
`OperId`,
`Result`,
`CreateTime`,
`ReviewStatus`,
`Title`)
VALUES
(
@OrgId,
@BranchId,
@NewsId,
@NewsType,
@OperId,
@Result,
@CreateTime,
@ReviewStatus,
@Title);

", mod);
        
        }

        public List<TbReview> GetAuditList(GetAuditRequest requset,int branchId, out int i)
        {
            var sql = new StringBuilder();
            var parameter = new StringBuilder();
            var dynamicParams = new DynamicParameters();
            var token = cacheHelper.GetData<TbUserToken>(requset.Token);

            if (token.OrgId > 0)
            {
                parameter.Append(" and  OrgId = @OrgId ");
                dynamicParams.Add("OrgId", token.OrgId);
            }


            if (branchId > 0)
            {
                parameter.Append(" and  BranchId = @BranchId ");
                dynamicParams.Add("BranchId", branchId);
            }



            sql.AppendFormat(@"SELECT count(*)
FROM `eagles`.`tb_review`  where 1=1  {0} ;
 ", parameter);
            i = dbManager.ExecuteScalar<int>(sql.ToString(), dynamicParams);

            sql.Clear();

            //if (requset.BranchId > 0)
            //{
            //    parameter.Append(" and BranchId = @BranchId ");
            //    dynamicParams.Add("BranchId", requset.BranchId);
            //}

            if (!string.IsNullOrWhiteSpace(requset.AuditName))
            {
                parameter.Append(" and Title like @Title ");
                dynamicParams.Add("Title", '%' + requset.AuditName + '%');
            }


            if (requset.StartTime != null)
            {
                parameter.Append(" and CreateTime >= @StartTime ");
                dynamicParams.Add("StartTime", requset.StartTime);
            }

            if (requset.EndTime != null)
            {
                parameter.Append(" and CreateTime <= @EndTime ");
                dynamicParams.Add("EndTime", requset.EndTime);
            }

            //if (requset.Status > 0)
            //{
            //    parameter.Append(" and Status = @Status ");
            //    dynamicParams.Add("Status", (int)requset.Status);
            //}


            sql.AppendFormat(@" SELECT `tb_review`.`ReviewId`,
    `tb_review`.`OrgId`,
    `tb_review`.`BranchId`,
    `tb_review`.`NewsId`,
    `tb_review`.`NewsType`,
    `tb_review`.`OperId`,
    `tb_review`.`Result`,
    `tb_review`.`CreateTime`,
    `tb_review`.`ReviewStatus`,
    `tb_review`.`Title`
FROM `eagles`.`tb_review`    where 1=1  {0}    order by CreateTime desc
 ", parameter);

            return dbManager.Query<TbReview>(sql.ToString(), dynamicParams);
        }

        public int Audit(string sql, int AuditId)
        {


            return dbManager.Excuted(sql,new {Id= AuditId });
           
        }

        public int GetScoreSet(int orgId, int branchId, int rewardType)
        {
            var sql = new StringBuilder();
            var parameter = new StringBuilder();
            var dynamicParams = new DynamicParameters();

            if (orgId > 0)
            {
                parameter.Append(" and OrgId = @OrgId ");
                dynamicParams.Add("OrgId", orgId);
            }

        

            //parameter.Append(" and  BranchId = @BranchId ");
            //dynamicParams.Add("BranchId", branchId);

            parameter.Append(" and  RewardType = @RewardType ");
            dynamicParams.Add("RewardType", rewardType);

            sql.AppendFormat(@" SELECT 
    `tb_reward_score`.`Score`
FROM `eagles`.`tb_reward_score`   where  1=1  {0}  
 ", parameter);


          
            return dbManager.ExecuteScalar<int>(sql.ToString(), dynamicParams);
            
        }

        public void SetUserScore(int score, int userId)
        {

            dbManager.Excuted(@"UPDATE `eagles`.`tb_user_info`
SET
`Score` =Score+@Score
where  UserId = @UserId

", new { UserId =userId, Score = score });
            
        }

        public void SendUserNotice(TbUserNotice notice)
        {

             dbManager.Excuted(@"INSERT INTO `eagles`.`tb_user_notice`
(`OrgId`,
`NewsId`,
`NewsType`,
`Title`,
`UserId`,
`Content`,
`TargetUrl`,
`IsRead`,
`FromUser`,
`CreateTime`,
`ReadTime`)
VALUES
(@OrgId,
@NewsId,
@NewsType,
@Title,
@UserId,
@Content,
@TargetUrl,
@IsRead,
@FromUser,
now(),
now());
", notice);
            
        }


        public int SendUserNotice(List<TbUserNotice> list)
        {

          return  dbManager.Excuted(@"INSERT INTO `eagles`.`tb_user_notice`
(`OrgId`,
`NewsId`,
`NewsType`,
`Title`,
`UserId`,
`Content`,
`TargetUrl`,
`IsRead`,
`FromUser`,
`CreateTime`,
`ReadTime`)
VALUES
(@OrgId,
@NewsId,
@NewsType,
@Title,
@UserId,
@Content,
@TargetUrl,
@IsRead,
@FromUser,
now(),
now());
", list);

        }

        public void UserScoreTrace(TbUserScoreTrace userScoreTrace)
        {
            dbManager.Excuted(@"INSERT INTO `eagles`.`tb_user_score_trace`
(`OrgId`,
`UserId`,
`TraceId`,
`CreateTime`,
`Score`,
`RewardsType`,
`Comment`,
`OriScore`)
VALUES
(@OrgId,
@UserId,
@TraceId,
now(),
@Score,
@RewardsType,
@Comment,
@OriScore);

", userScoreTrace);
            
        }

        public List<TbRewardScore> GetScoreSetInfo(int tokenOrgId)
        {
            var sql = new StringBuilder();
            var parameter = new StringBuilder();
            var dynamicParams = new DynamicParameters();

            parameter.Append(" and OrgId = @OrgId ");
            dynamicParams.Add("OrgId", tokenOrgId);

            sql.AppendFormat(@" SELECT `tb_reward_score`.`RewardId`,
    `tb_reward_score`.`OrgId`,
    `tb_reward_score`.`BranchId`,
    `tb_reward_score`.`RewardType`,
    `tb_reward_score`.`Score`,
    `tb_reward_score`.`keyWord`,
    `tb_reward_score`.`LearnTime`,
    `tb_reward_score`.`WordCount`,
    `tb_reward_score`.`Status`
FROM `eagles`.`tb_reward_score`    where  1=1  {0}  
 ", parameter);

            return dbManager.Query<TbRewardScore>(sql.ToString(), dynamicParams);
        }

        public void UserScoreTrace(List<TbUserScoreTrace> ls)
        {


            dbManager.Excuted(@"INSERT INTO `eagles`.`tb_user_score_trace`
(`OrgId`,
`UserId`,
`TraceId`,
`CreateTime`,
`Score`,
`RewardsType`,
`Comment`,
`OriScore`,
`Source`)
VALUES
(@OrgId,
@UserId,
@TraceId,
now(),
@Score,
@RewardsType,
@Comment,
@OriScore,
@Source);

", ls);

        }

        public void SetUserScore(int score, List<int> list)
        {

            dbManager.Excuted(@"UPDATE `eagles`.`tb_user_info`
SET
`Score` =Score+@Score
where  UserId in @UserId

", new { UserId = list.ToArray(), Score = score });
        }
    }
}
